﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Structure;
using YArchitech.LIB;

namespace YArchitech.Revit
{
	public static class RevitElementExtension
	{
		public static IList<Curve> ComputeRebarDrivingCurves(this Rebar inputRebar)
		{
			return inputRebar.ComputeDrivingCurves();
		}

		public static ElementId GetBoundarySegmentElementId(BoundarySegment inputBoundarySegment, out ElementId linkedElementId)
		{
			linkedElementId = null;
			ElementId elementId = inputBoundarySegment.ElementId;
			linkedElementId = inputBoundarySegment.LinkElementId;
			return elementId;
		}

		public static Parameter GetParameter(this Element elem, BuiltInParameter parameterId)
		{
			return elem.get_Parameter(parameterId);
		}

		public static Parameter GetParameter(this Element elem, Definition definition)
		{
			return elem.get_Parameter(definition);
		}

		public static Parameter GetParameter(this Element elem, Guid guid)
		{
			return elem.get_Parameter(guid);
		}

		public static string GetMark(this Element elem)
		{
			Parameter parameter = elem.get_Parameter(BuiltInParameter.DOOR_NUMBER);
			if (parameter == null)
			{
				return string.Empty;
			}
			return parameter.AsString();
		}

		public static Parameter GetParameter(this Element elem, string paramName)
		{
			return elem.LookupParameter(paramName);
		}

		public static Level GetYJKLevel(this Element elem)
		{
			return elem.Document.GetElementById(elem.LevelId) as Level;
		}

		public static string GetFamilyNameBySymbol(this FamilySymbol fs)
		{
			return fs.FamilyName;
		}

		public static void GetTextNotePosition(ref XYZ pt, TextAlignFlags txtAlign, double dCellHeight, double dCellWidth, double txtHeight, double dpiY, double scale)
		{
			double num = 2.0 / dpiY * 0.08333;
			if (txtAlign <= 1088)
			{
				if (txtAlign <= 640)
				{
					if (txtAlign == 576)
					{
						pt += XYZ.BasisY * dCellHeight;
						pt += XYZ.BasisX * num * scale;
						pt -= XYZ.BasisY * num * scale;
						return;
					}
					if (txtAlign == 640)
					{
						pt += XYZ.BasisX * 0.5 * dCellWidth;
						pt += XYZ.BasisY * dCellHeight;
						pt -= XYZ.BasisY * num * scale;
						return;
					}
				}
				else
				{
					if (txtAlign == 768)
					{
						pt += XYZ.BasisX * dCellWidth;
						pt += XYZ.BasisY * dCellHeight;
						pt -= XYZ.BasisX * num * scale;
						pt -= XYZ.BasisY * num * scale;
						return;
					}
					if (txtAlign == 1088)
					{
						pt += XYZ.BasisY * 0.5 * (dCellHeight + txtHeight);
						pt += XYZ.BasisX * num * scale;
						return;
					}
				}
			}
			else if (txtAlign <= 1280)
			{
				if (txtAlign == 1152)
				{
					pt += XYZ.BasisX * 0.5 * dCellWidth;
					pt += XYZ.BasisY * 0.5 * (dCellHeight + txtHeight);
					return;
				}
				if (txtAlign == 1280)
				{
					pt += XYZ.BasisX * dCellWidth;
					pt += XYZ.BasisY * 0.5 * (dCellHeight + txtHeight);
					pt -= XYZ.BasisX * num * scale;
					return;
				}
			}
			else
			{
				if (txtAlign == 2112)
				{
					pt += XYZ.BasisY * txtHeight;
					pt += XYZ.BasisX * num * scale;
					pt += XYZ.BasisY * num * scale;
					return;
				}
				if (txtAlign == 2176)
				{
					pt += XYZ.BasisY * txtHeight;
					pt += XYZ.BasisX * 0.5 * dCellWidth;
					pt += XYZ.BasisY * num * scale;
					return;
				}
				if (txtAlign == 2304)
				{
					pt += XYZ.BasisX * dCellWidth;
					pt += XYZ.BasisY * txtHeight;
					pt -= XYZ.BasisX * num * scale;
					pt += XYZ.BasisY * num * scale;
					return;
				}
			}
			pt += XYZ.BasisY * dCellHeight;
			pt += XYZ.BasisX * num * scale;
			pt -= XYZ.BasisY * num * scale;
		}

		public static XYZ GetTextNotePosition(XYZ ptText, int quar, double dH, double dWidth, Autodesk.Revit.DB.View view)
		{
			XYZ xyz = ptText;
			if (quar == 1)
			{
				xyz += XYZ.BasisX * 60.0 / 304.8;
			}
			else if (quar == 3)
			{
				XYZ xyz2 = -XYZ.BasisY * dH * (double)view.Scale;
				xyz += xyz2;
			}
			else if (quar == 4)
			{
				XYZ xyz2
